package com.taxi.management.controller;


import com.taxi.management.entity.DriverEntity;
import com.taxi.management.entity.ResultEntity;
import com.taxi.management.service.Impl.DriverServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v3/driver/")
@CrossOrigin//解决跨域问题
@RestController//Json
public class DriverController {
    @Resource
    DriverServiceImpl driverService;

    @GetMapping("getAllDriver")
    public ResultEntity getAllDriver() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.getAllDriver());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(driverService.getAllDriver().size());
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getDriverById")
    public ResultEntity getDriverById(@RequestParam int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.getDriverById(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PostMapping("addDriver")
    public ResultEntity addDriver(@RequestBody DriverEntity driver) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.addDriver(driver));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PutMapping("updateDriver")
    public ResultEntity updateDriver(@RequestBody DriverEntity driver) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.updateDriver(driver));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @DeleteMapping("deleteDriver/{id}")
    public ResultEntity deleteDriver(@PathVariable int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.deleteDriver(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getDriverByPage")
    public ResultEntity getDriverByPage(@RequestParam String username, @RequestParam int start, @RequestParam int limit) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(driverService.getDriverByPage(username, start, limit));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(driverService.getDriverByPage(username, start, limit).size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

}
